/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sasp_messages;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author mauriello
 */
public abstract class SASPReply {
    public final static String ACK      = "ACK";
    public final static String REQ_SN   = "REQ_SN";
    public final static String REQ_TBL  = "REQ_TBL";
    public final static String REQ_DATA = "REQ_DATA";
    public final static String NACK     = "NACK";
    private String type;
    private int originatorID;
    private int destinationID;
    private int replyToID;
    private Date dateTime;
    private JSONObject replyBody;
    private JSONObject theMessage;

    public void setDateTime( ) {
        dateTime = new Date( );
    }

    public Date getDateTime( ) {
        return dateTime;
    }

    public void setType( String _t ) {
        type = _t;
    }

    public String getType( ) {
        return type;
    }

    public void setReplyBody( JSONObject _r ) {
        replyBody = _r;
    }

    public JSONObject getReplyBody( ) {
        return replyBody;
    }

    public void setOriginatorID( int _o ) {
        originatorID = _o;
    }

    public int getOriginatorID( ) {
        return originatorID;
    }

    public void setReplyToID( int _r ) {
        replyToID = _r;
    }

    public int getReplyToID( ) {
        return replyToID;
    }

    public void setDestinationID( int _d ) {
        destinationID = _d;
    }

    public int getDestinationID( ) {
        return destinationID;
    }

    public SASPReply( ) {
        theMessage = new JSONObject();
        replyBody = new JSONObject();
        dateTime = new Date();
    }

    public SASPReply(int _o, int _d, int _r ) {
        this();
        originatorID = _o;
        destinationID = _d;
        replyToID = _r;
    }

    public SASPReply( String _inJson ) {
        try {
            theMessage = new JSONObject(_inJson);
            replyBody = theMessage.getJSONObject("ReplyBody");
            originatorID = theMessage.getInt("OriginatorID");
            destinationID = theMessage.getInt("DestinationID");
            replyToID = theMessage.getInt("ReplyToID");
            type = theMessage.getString("SASPv1Reply");
            /* TODO: Parse Date/Time */
        } catch (JSONException ex) {
            Logger.getLogger(SASPCommand.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String toString( ) {
        boolean trouble = false;
        JSONObject retVal = new JSONObject();
        try {
            retVal.put("SASPv1Reply", this.getType());
            retVal.put("OriginatorID", Integer.toString(this.getOriginatorID()));
            retVal.put("DestinationID", Integer.toString(this.getDestinationID()));
            retVal.put("ReplyToID", Integer.toString(this.getReplyToID()));
            retVal.put("Timestamp", this.getDateTime().toString());
            retVal.put("ReplyBody", this.getReplyBody());
        } catch (JSONException ex) {
            trouble = true;
            Logger.getLogger(PINGCommand.class.getName()).log(Level.SEVERE, null, ex);
        }
        if( trouble ) {
            return null;
        } else {
            return retVal.toString();
        }
    }

    abstract public void updateReplyBody( );
}
